<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class SiteMap_Controller extends CI_Controller {
	
	function __construct()
    {
		parent::__construct();
		$this->load->model('product_model');
    }
	
	public function index()
	{
		show_sitemap();
	}
	
	public function show_sitemap()
	{
		$last_modified_time = strtotime('2011-08-01 01:01');
		$cache_time_in_seconds = 2592000; // 30 days 60*60*24*30
		http_not_modified($last_modified_time);
		set_http_cache_headers($last_modified_time, $cache_time_in_seconds);
			
		$this->template->set_template('simple');
		//$this->css_head = array ('/css/main_simple.css');
		$this->load->model('product_model');
		$this->template->write_view('header', 'header_detail_block', NULL, FALSE);
		$this->template->write_view('content', 'sitemap_html_block', array('categories' => $this->product_model->get_category_list()), FALSE);	
		$this->template->write_view('footer', 'footer_default_block', NULL, FALSE);
		//$this->output->cache(43200);
		$this->render_template = TRUE;	

	}
	public function generate_sitemap()
	{
		
		$this->load->helper('file');

		$start = 0; 
		$limit = 20000;
		$sitemap_filenames = array();
		
		$loop_count = 0;
		while (TRUE)
		{ 
			$products = $this->product_model->get_recent_products($start, $limit);
			
			// break loop if no more products
			if($products->num_rows() == 0)
			{
				break;
			}
			
			$loop_count = $loop_count + 1;
			$relatve_file_path = "/sitemap_product_part_${loop_count}.xml";
			$sitemap_filenames[$loop_count] = $relatve_file_path;

			// write site map
			$data = $this->load->view('sitemap_block', array('products' => $products, 'changefreq' => 'weekly', 'priority' => '0.1'), TRUE);
			echo 'generating site map ... ', BASEPATH, '/..', $relatve_file_path, '<br/>';
			write_file(BASEPATH .'/..'.$relatve_file_path, $data, 'w');
			$start = $start + $limit;
		}
		
		// write sitemap index file
		echo 'generating site mape index ... <br/>';
		$data = $this->load->view('sitemap_index_block', array('sitemap_filenames' => $sitemap_filenames), TRUE);
		write_file(BASEPATH .'/../sitemap.xml', $data, 'w');
	}
}

/* End of file sitemap_controller.php */
/* Location: ./application/controllers/SiteMap_Controller.php */